package com.huirui.server.controller.mini.program;

import com.huirui.server.pojo.dto.VerificationCodeLoginDTO;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.UserBriefInfoVo;
import com.huirui.server.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@Slf4j
@Validated
@Tag(name = "用户管理", description = "微信小程序用户管理相关")
public class UserController {

    @Resource
    private UserService userService;



    @GetMapping("/code")
    @Operation(summary = "获取手机验证码")
    public Result<String> getVerificationCode(
            @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确，请输入11位有效手机号")
            @NotBlank(message = "手机号不能为空")
            @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确，请输入11位有效手机号")
            @Pattern(regexp = "^(?!17[01]\\d{8}$).*$", message = "暂不支持170/171号段的手机号")
            @RequestParam
            String phone) {
        String verificationCode = userService.getVerificationCode(phone);
        return Result.success(verificationCode);
    }

    @PostMapping("/login/code")
    @Operation(summary = "验证码登录/注册")
    public Result<String> loginByVerificationCode(
            @Valid
            @RequestBody
            VerificationCodeLoginDTO loginDTO) {
        String token = userService.loginByVerificationCode(loginDTO);
        return Result.success(token);
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户页信息")
    public Result<UserBriefInfoVo> getUserBriefInfo() {
        UserBriefInfoVo userBriefInfoVo = userService.getUserBriefInfo();
        return Result.success(userBriefInfoVo);
    }

    @GetMapping("/login/wechat")
    @Operation(summary = "通过微信小程序的openId进行登录")
    public Result<String> loginByWeChatOpenid(@RequestParam String code) {
        String token = userService.loginByWeChatOpenid(code);
        return Result.success(token);
    }


    @PostMapping("/quickly/login")
    @Operation(summary = "快捷登录")
    public Result<String> loginQuickly(@RequestParam("code") String code) {
        String token = userService.loginQuickly(code);
        return Result.success(token);
    }

}
